package com.southminority.ethnic.controller.student;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.controller.student.vo.AwardPageVo;
import com.southminority.ethnic.controller.student.vo.StudentPageReqVo;
import com.southminority.ethnic.pojo.StudentAward;
import com.southminority.ethnic.service.student.StudentAwardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 学生获奖用户端控制器
 * 
 * @author SouthMinority
 */
@Slf4j
@Tag(name = "学生获奖模块（用户端）", description = "学生获奖查询接口")
@RestController
@RequestMapping("/student/award")
@CrossOrigin
public class StudentAwardController {
    
    @Autowired
    private StudentAwardService studentAwardService;

    /**
     * 分页查询学生获奖列表（优化版，不返回完整描述）
     * @param reqVo 分页请求参数
     * @return 分页结果
     */
    @Operation(summary = "分页查询学生获奖列表")
    @PostMapping("/page")
    public R<Page<AwardPageVo>> getAwardPage(@Valid @RequestBody StudentPageReqVo reqVo) {
        try {
            Page<AwardPageVo> result = studentAwardService.getAwardPage(
                reqVo.getPageNum(), 
                reqVo.getPageSize(), 
                reqVo.getKeyword(),
                    1
            );
            return R.ok(result);
        } catch (Exception e) {
            log.error("查询学生获奖列表失败", e);
            return R.fail("查询失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID查询学生获奖详情（包含完整描述）
     * @param id 获奖ID
     * @return 获奖详情
     */
    @Operation(summary = "查询学生获奖详情")
    @GetMapping("/{id}")
    public R<StudentAward> getAwardById(@PathVariable Long id) {
        try {
            StudentAward award = studentAwardService.getById(id);
            if (award == null || award.getIsDeleted() == 1 || award.getStatus() != 1) {
                return R.fail("获奖信息不存在或已下架");
            }
            // 增加浏览次数
            studentAwardService.incrementViews(id);
            return R.ok(award);
        } catch (Exception e) {
            log.error("查询学生获奖详情失败", e);
            return R.fail("查询失败: " + e.getMessage());
        }
    }
}
